<?php

namespace app\admin\controller;

use app\common\lib\IAuth;
use think\Cache;
use think\Controller;
use think\Request;
use think\Session;

class Admin extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $admin = model('admin')->getAdminInfoAll();
        return $this->fetch('',['admin'=>$admin]);
    }

    /**
     * 添加管理员
     *
     * @return \think\Response
     */
    public function add()
    {
        if(\request()->isPost()){
            $data = input('post.');
            $validate = validate('admin');
            if(!$validate->scene('add')->check($data)){
                $this->result($_SERVER['HTTP_REFERER'],config('code.error'),$validate->getError());
            }else{
                //如果同时选择权限和分组
                if($data['group'] && $user = Cache::store('redis')->get(config('redis.save_menus').'_'.Session::get(config('user.user_id')))){
                    $group = model('AuthorityGroup')->getMealAll($data['group']);
                    //如果都有逗号分隔
                    if(preg_match('/,/',$group->menus) && preg_match('/,/',$user)){
                        $arr = explode(',',$group->menus);
                        $user = explode(',',$user);
                        foreach($arr as $v){
                            if(!in_array($v,$user)){
                                array_push($user,$v);
                            }
                            $data['menus'] = implode(',',$user);
                            $data['last_time'] = time();
                            $data['last_ip'] = \request()->ip();
                            $data['pwd'] = IAuth::setPassword($data['pwd']);
                        }
                        //权限没有逗号，分组有逗号
                    }else if(!preg_match('/,/',$group->menus) && preg_match('/,/',$user)){
                        $user = explode(',',$user);
                        if(!in_array($group->menus,$user)){
                            array_push($user,$group->menus);
                        }
                        $data['menus'] = implode(',',$user);
                        $data['last_time'] = time();
                        $data['last_ip'] = \request()->ip();
                        $data['pwd'] = IAuth::setPassword($data['pwd']);
                        //分组有逗号，权限没有逗号
                    }else if(preg_match('/,/',$group->menus) && !preg_match('/,/',$user)){
                        $arr = explode(',',$group->menus);
                        if(!in_array($user,$arr)){
                            array_push($arr,$user);
                        }
                        $data['menus'] = implode(',',$arr);
                        $data['last_time'] = time();
                        $data['last_ip'] = \request()->ip();
                        $data['pwd'] = IAuth::setPassword($data['pwd']);
                    }
                    $id = model('admin')->allowField(true)->add($data);
                    if($id){
                        $this->result($_SERVER['HTTP_REFERER'],config('code.success'),'添加成功');
                    }else{
                        $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'添加失败');
                    }
                    //选择分组没有选择权限
                }else if(empty($data['group']) && $user = Cache::store('redis')->get(config('redis.save_menus').'_'.Session::get(config('user.user_id')))){
                    $data['menus'] = $user;
                    $data['last_time'] = time();
                    $data['last_ip'] = \request()->ip();
                    $data['pwd'] = IAuth::setPassword($data['pwd']);
                    $id = model('admin')->allowField(true)->add($data);
                    if($id){
                        $this->result($_SERVER['HTTP_REFERER'],config('code.success'),'添加成功');
                    }else{
                        $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'添加失败');
                    }
                    //选择权限没有选择分组
                }else if(!$user = Cache::store('redis')->get(config('redis.save_menus').'_'.Session::get(config('user.user_id'))) && $data['group']){
                    $group = model('AuthorityGroup')->getMealAll($data['group']);
                    if(!empty($group)){
                        $data['menus'] = $group->menus;
                        $data['last_time'] = time();
                        $data['last_ip'] = \request()->ip();
                        $data['pwd'] = IAuth::setPassword($data['pwd']);
                        $id = model('admin')->allowField(true)->add($data);
                        if($id){
                            $this->result($_SERVER['HTTP_REFERER'],config('code.success'),'添加成功');
                        }else{
                            $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'添加失败');
                        }
                    }else{
                        $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'权限分组不存在');
                    }
                }
            }
        }else{
            //查询所有权限组
            $group = model('authority_group')->select();
            return $this->fetch('',['group'=>$group]);
        }
    }

    /**
     * 权限列表
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function admin_menus()
    {
        if(\request()->isPost()){
            $menus = input('post.menus/a');
            if(empty($menus)){
                $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'请选择权限');
            }
            $menus = implode(',',$menus);
            Cache::store('redis')->set(config('redis.save_menus').'_'.Session::get(config('user.user_id')),$menus);
            $this->result($_SERVER['HTTP_REFERER'],config('code.success'),'选择成功');
        }else{
            return $this->fetch();
        }
    }

    /**
     * 编辑管理员
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit()
    {
        if(\request()->isPost()){
            $data = input('post.');
            $validate = validate('admin');
            if(!$validate->scene('add')->check($data)){
                $this->result($_SERVER['HTTP_REFERER'],config('code.error'),$validate->getError());
            }
            if(!empty($data['status']) && $data['status'] == 'on'){
                $data['status'] = 1;
            }else{
                $data['status'] = 0;
            }
            $id = model('admin')->allowField(true)->save($data,['id'=>$data['id']]);
            if($id){
                $this->result($_SERVER['HTTP_REFERER'],config('code.success'),'修改成功');
            }else{
                $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'修改失败');
            }
        }else{
            $id = input('get.id',0,'intval');
            if(!$id){
                $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'参数错误');
            }
            $admin = model('admin')->getAdminInfoAll($id);
            return $this->fetch('',['admin'=>$admin,'id'=>$id]);
        }
    }


    /**
     * 修改管理员权限
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function edit_menus()
    {
        $id = input('param.id',0,'intval');
        if(\request()->isPost()){
            //接收权限数组参数
            $menus = input('post.menus/a');
            if(empty($menus)){
                $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'请选择权限');
            }
            $id = model('admin')->allowField(true)->save(['menus'=>$menus[0]],['id'=>$id]);
            if($id){
                $this->result($_SERVER['HTTP_REFERER'],config('code.success'),'修改成功');
            }else{
                $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'修改失败');
            }
        }else{
            if(empty($id)){
                return '参数错误';
            }
            return $this->fetch('',['id'=>$id]);
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
